package com.sunshine.wrongquestionset.room.dao

import androidx.lifecycle.LiveData
import androidx.room.*
import com.sunshine.wrongquestionset.room.entity.SubjectEntity
import javax.security.auth.Subject

/**
 * @author sunshine
 * @date 2021/3/26
 * @see com.sunshine.wrongquestionset.room.entity.SubjectEntity
 */
@Dao
interface SubjectDao {
    //插入
    @Query("INSERT INTO subject(userId, name) VALUES(:userId,:name)")
    fun insert(userId: String, name: String)

    @Insert(entity = SubjectEntity::class, onConflict = OnConflictStrategy.IGNORE)
    fun insertByEntity(subjectEntity: SubjectEntity)

    //通过实体删除
    @Delete(entity = SubjectEntity::class)
    fun deleteByEntity(subjectEntity: SubjectEntity?)

    //通过名字删除
    @Query("DELETE FROM subject WHERE userId=:userId and name=:name")
    fun deleteByName(userId: String, name: String)

    //查询所有
    @Query("SELECT * FROM subject WHERE userId=:userId")
    fun selectAllByUserId(userId: String): LiveData<List<SubjectEntity>?>

    @Query("SELECT * FROM subject WHERE id=:id")
    fun selectById(id: Int): LiveData<SubjectEntity?>

    //更新
    @Update(entity = SubjectEntity::class, onConflict = OnConflictStrategy.REPLACE)
    fun update(subjectEntity: SubjectEntity?)
}